package com.yst.suivi.data.rights;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.ejb.CreateException;
import javax.naming.NamingException;

import com.yst.suivi.services.rights.RightView;
import com.yst.suivi.services.rights.RightViewUtil;

public class Right implements Serializable
{
	public static final Right CREATE_ACTIVITY = new ProjectRight(1);

	public static final Right EDIT_ACTIVITY = new ProjectRight(2);

	public static final Right DELETE_ACTIVITY = new ProjectRight(3);

	public static final Right CLOSE_ACTIVITY = new ProjectRight(4);

	public static final Right REPORT_ACTIVITY = new ProjectRight(5);

	public static final Right REPORT_ACTIVITY_FOR = new ProjectRight(6);

	public static final Right CLOSE_POINTAGE_FOR = new ProjectRight(7);

	public static final Right OPEN_POINTAGE_FOR = new ProjectRight(8);

	public static final Right REPORT_YEARLY_ACTIVITY = new ProjectRight(9);

	public static final Right ASSIGN_ACTIVITY = new ProjectRight(10);

	public static final Right CREATE_PROJECT_VERSION = new ProjectRight(11);

	public static final Right EDIT_PROJECT_VERSION = new ProjectRight(12);

	public static final Right VIEW_ADMIN_PROJECT_VERSION = new ProjectRight(13);

	public static final Right CLOSE_PROJECT_VERSION = new ProjectRight(14);

	public static final Right OPEN_PROJECT_VERSION = new ProjectRight(15);

	public static final Right CREATE_PROJECT = new Right(21);

	public static final Right EDIT_PROJECT = new Right(22);

	public static final Right DELETE_PROJECT = new Right(23);

	public static final Right CLOSE_PROJECT = new Right(24);

	public static final Right VIEW_ADMIN_PROJECT = new Right(26);

	public static final Right EDIT_TEAM_PROJECT = new Right(25);

	public static final Right CREATE_FOLDER = new ProjectRight(31);

	public static final Right EDIT_FOLDER = new ProjectRight(32);

	public static final Right CREATE_USER = new Right(41);

	public static final Right EDIT_USER = new Right(42);

	public static final Right DELETE_USER = new Right(43);

	public static final Right VIEW_ADMIN_USER = new Right(44);

	public static final Right CREATE_VACATION = new Right(51);

	public static final Right EDIT_VACATION = new Right(52);

	public static final Right VIEW_ADMIN_VACATION = new Right(53);

	public static final Right CREATE_PROFIL = new Right(61);

	public static final Right EDIT_PROFIL = new Right(62);

	public static final Right ADMIN_VIEW_PROFIL = new Right(63);

	public static final Right VIEW_POINTAGE = new ProjectRight(71);

	public static final Right VIEW_MY_ACTIVITY = new ProjectRight(72);

	public static final Right CREATE_GENERAL_REPORT = new ProjectRight(80);

	public static final Right DELETE_GENERAL_REPORT = new ProjectRight(81);

	public static final Right GENERATE_GENERAL_REPORT = new ProjectRight(82);

	public static final Right CREATE_TEAM = new Right(91);

	public static final Right EDIT_TEAM = new Right(92);

	public static final Right DELETE_TEAM = new Right(93);

	public static final Right VIEW_ADMIN_TEAM = new Right(94);

	public static final Right CREATE_LOT = new ProjectRight(101);

	public static final Right EDIT_LOT = new ProjectRight(102);

	public static final Right DELETE_LOT = new ProjectRight(103);
	
	public static final Right FINANCIAL_VIEW = new ProjectRight(111);

	public static final Right REFRESH = new Right(0);

	private int mCode;

	private static ArrayList mLstRights = null;

	private static ResourceBundle bundle = null;
	static
	{
		bundle = ResourceBundle.getBundle("com.yst.suivi.data.rights.Rights", Locale.ENGLISH);
	}

	/**
	 * Constructeur prive pour etre oblige de passer par les constantes.
	 * 
	 */
	protected Right(int code)
	{
		mCode = code;
	}

	public static Right get(int code)
	{
		Right retour = null;
		switch (code)
		{
		case 0:
			retour = REFRESH;
			break;
		case 1:
			retour = CREATE_ACTIVITY;
			break;
		case 2:
			retour = EDIT_ACTIVITY;
			break;
		case 3:
			retour = DELETE_ACTIVITY;
			break;
		case 4:
			retour = CLOSE_ACTIVITY;
			break;
		case 5:
			retour = REPORT_ACTIVITY;
			break;
		case 6:
			retour = REPORT_ACTIVITY_FOR;
			break;
		case 7:
			retour = CLOSE_POINTAGE_FOR;
			break;
		case 8:
			retour = OPEN_POINTAGE_FOR;
			break;
		case 9:
			retour = REPORT_YEARLY_ACTIVITY;
			break;
		case 10:
			retour = ASSIGN_ACTIVITY;
			break;
		case 11:
			retour = CREATE_PROJECT_VERSION;
			break;
		case 12:
			retour = EDIT_PROJECT_VERSION;
			break;
		case 13:
			retour = VIEW_ADMIN_PROJECT_VERSION;
			break;
		case 14:
			retour = CLOSE_PROJECT_VERSION;
			break;
		case 15:
			retour = OPEN_PROJECT_VERSION;
			break;
		case 21:
			retour = CREATE_PROJECT;
			break;
		case 22:
			retour = EDIT_PROJECT;
			break;
		case 23:
			retour = DELETE_PROJECT;
			break;
		case 24:
			retour = CLOSE_PROJECT;
			break;
		case 25:
			retour = EDIT_TEAM_PROJECT;
			break;
		case 26:
			retour = VIEW_ADMIN_PROJECT;
			break;
		case 31:
			retour = CREATE_FOLDER;
			break;
		case 32:
			retour = EDIT_FOLDER;
			break;
		case 41:
			retour = CREATE_USER;
			break;
		case 42:
			retour = EDIT_USER;
			break;
		case 43:
			retour = DELETE_USER;
			break;
		case 44:
			retour = VIEW_ADMIN_USER;
			break;
		case 51:
			retour = CREATE_VACATION;
			break;
		case 52:
			retour = EDIT_VACATION;
			break;
		case 53:
			retour = VIEW_ADMIN_VACATION;
			break;
		case 61:
			retour = CREATE_PROFIL;
			break;
		case 62:
			retour = EDIT_PROFIL;
			break;
		case 63:
			retour = ADMIN_VIEW_PROFIL;
			break;
		case 71:
			retour = VIEW_POINTAGE;
			break;
		case 72:
			retour = VIEW_MY_ACTIVITY;
			break;
		case 80:
			retour = CREATE_GENERAL_REPORT;
			break;
		case 81:
			retour = DELETE_GENERAL_REPORT;
			break;
		case 82:
			retour = GENERATE_GENERAL_REPORT;
			break;
		case 91:
			retour = CREATE_TEAM;
			break;
		case 92:
			retour = EDIT_TEAM;
			break;
		case 93:
			retour = DELETE_TEAM;
			break;
		case 94:
			retour = VIEW_ADMIN_TEAM;
			break;
		case 101:
			retour = CREATE_LOT;
			break;
		case 102:
			retour = EDIT_LOT;
			break;
		case 103:
			retour = DELETE_LOT;
			break;
		case 111:
			retour = FINANCIAL_VIEW;
			break;
		default:
			System.out.println("Missing " + code);
			break;
		}
		return retour;
	}

	public static List getElements()
	{
		buildList();
		return mLstRights;
	}

	private static void buildList()
	{
		if (null == mLstRights)
		{
			mLstRights = new ArrayList();
			mLstRights.add(CREATE_ACTIVITY);
			mLstRights.add(EDIT_ACTIVITY);
			mLstRights.add(DELETE_ACTIVITY);
			mLstRights.add(CLOSE_ACTIVITY);
			mLstRights.add(REPORT_ACTIVITY);
			mLstRights.add(REPORT_ACTIVITY_FOR);
			mLstRights.add(CLOSE_POINTAGE_FOR);
			mLstRights.add(OPEN_POINTAGE_FOR);
			mLstRights.add(REPORT_YEARLY_ACTIVITY);
			mLstRights.add(ASSIGN_ACTIVITY);
			mLstRights.add(CREATE_PROJECT_VERSION);
			mLstRights.add(EDIT_PROJECT_VERSION);
			mLstRights.add(VIEW_ADMIN_PROJECT_VERSION);
			mLstRights.add(CLOSE_PROJECT_VERSION);
			mLstRights.add(OPEN_PROJECT_VERSION);
			mLstRights.add(CREATE_PROJECT);
			mLstRights.add(EDIT_PROJECT);
			mLstRights.add(DELETE_PROJECT);
			mLstRights.add(CLOSE_PROJECT);
			mLstRights.add(VIEW_ADMIN_PROJECT);
			mLstRights.add(EDIT_TEAM_PROJECT);
			mLstRights.add(CREATE_FOLDER);
			mLstRights.add(EDIT_FOLDER);
			mLstRights.add(CREATE_USER);
			mLstRights.add(EDIT_USER);
			mLstRights.add(DELETE_USER);
			mLstRights.add(VIEW_ADMIN_USER);
			mLstRights.add(CREATE_VACATION);
			mLstRights.add(EDIT_VACATION);
			mLstRights.add(VIEW_ADMIN_VACATION);
			mLstRights.add(CREATE_PROFIL);
			mLstRights.add(EDIT_PROFIL);
			mLstRights.add(ADMIN_VIEW_PROFIL);
			mLstRights.add(VIEW_POINTAGE);
			mLstRights.add(VIEW_MY_ACTIVITY);
			mLstRights.add(CREATE_GENERAL_REPORT);
			mLstRights.add(DELETE_GENERAL_REPORT);
			mLstRights.add(GENERATE_GENERAL_REPORT);
			mLstRights.add(CREATE_TEAM);
			mLstRights.add(EDIT_TEAM);
			mLstRights.add(DELETE_TEAM);
			mLstRights.add(VIEW_ADMIN_TEAM);
			mLstRights.add(CREATE_LOT);
			mLstRights.add(EDIT_LOT);
			mLstRights.add(DELETE_LOT);
			mLstRights.add(FINANCIAL_VIEW);
		}
	}

	/**
	 * @return the code
	 */
	protected Integer getCode()
	{
		return new Integer(mCode);
	}

	/**
	 * Verifie que l'utilisateur courant peut executer une commande.
	 * 
	 * @return
	 */
	public boolean canDo()
	{
		boolean retour = false;
		try
		{
			RightView view = RightViewUtil.getLocalHome().create();
			retour = view.canDo(this);
		}
		catch (CreateException e)
		{
			retour = false;
		}
		catch (NamingException e)
		{
			retour = false;
		}
		return retour;
	}

	/**
	 * The method returns the name of the state. The name is issued of the
	 * ressource file Status.properties
	 * 
	 * @param status
	 * @return
	 */
	public final String getName()
	{
		String retour = Integer.toString(mCode);
		try
		{
			retour = bundle.getString(getCode().toString());
		}
		catch (java.util.MissingResourceException e)
		{

		}
		return retour;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj)
	{
		boolean retour = false;
		// test if the given object is a Rights object
		if (obj.getClass().getName().equals(this.getClass().getName()))
		{
			Right r = (Right) obj;
			retour = r.getCode().equals(this.getCode());
		}
		return retour;
	}

	/**
	 * Indique si le droit s'applique a un projet ou pas
	 * 
	 * @return
	 */
	public boolean isProjectRight()
	{
		return false;
	}
}
